﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Administration.Catalog;
using Assesment.Library.Enum;
using Assesment.Library.Interfaces;
using Assesment.Library.DomainObject.Administration.Catalog;

namespace Assesment.Library.DataAccess.Administration.Catalog
{
    public partial class ManagerialKeyBehaviourRepo : ISearchable<ManagerialKeyBehaviour>
    {
        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {

                        case SQL.Function.Aggregate.Count:
                            cmd.CommandText = "SP_ManagerialKeyBehaviour_GridPage_CountData";
                            break;

                        default:
                            // do nothing 
                            break;
                    }


                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@column", SqlDbType.VarChar).Value = column;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = parameters;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }



        public List<ManagerialKeyBehaviour> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<ManagerialKeyBehaviour> tt = new List<ManagerialKeyBehaviour>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);


                    cmd.CommandText = "SP_ManagerialKeyBehaviour_GridPage";
                    cmd.Parameters.Add("@startRow", SqlDbType.Int).Value = startRow;
                    cmd.Parameters.Add("@endRow", SqlDbType.Int).Value = endRow;
                    cmd.Parameters.Add("@sortName", SqlDbType.VarChar).Value = sortName;
                    cmd.Parameters.Add("@sortOrder", SqlDbType.VarChar).Value = sortOrder;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = parameters;
                    cmd.CommandType = CommandType.StoredProcedure;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                ManagerialKeyBehaviour t = new ManagerialKeyBehaviour(true);
                                t.ManagerialKeyBehaviourID = dr[ManagerialKeyBehaviour.Fields.ManagerialKeyBehaviourID] == DBNull.Value ? 0 : int.Parse(dr[ManagerialKeyBehaviour.Fields.ManagerialKeyBehaviourID].ToString());
                                t.PositionID = dr[ManagerialKeyBehaviour.Fields.PositionID] == DBNull.Value ? (int?)null : int.Parse(dr[ManagerialKeyBehaviour.Fields.PositionID].ToString());
                                t.CompetencyID = dr[ManagerialKeyBehaviour.Fields.CompetencyID] == DBNull.Value ? (int?)null : int.Parse(dr[ManagerialKeyBehaviour.Fields.CompetencyID].ToString());
                                t.LevelCompetencyID = dr[ManagerialKeyBehaviour.Fields.LevelCompetencyID] == DBNull.Value ? (int?)null : int.Parse(dr[ManagerialKeyBehaviour.Fields.LevelCompetencyID].ToString());
                                t.CreatedDate = dr[ManagerialKeyBehaviour.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[ManagerialKeyBehaviour.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[ManagerialKeyBehaviour.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[ManagerialKeyBehaviour.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[ManagerialKeyBehaviour.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[ManagerialKeyBehaviour.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[ManagerialKeyBehaviour.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[ManagerialKeyBehaviour.Fields.UpdatedBy].ToString();


                                t.Department.DepartmentDesc = dr[Department.Fields.DepartmentDesc] == DBNull.Value ? string.Empty : dr[Department.Fields.DepartmentDesc].ToString();
                                t.Position.PositionName = dr[Position.Fields.PositionName] == DBNull.Value ? string.Empty : dr[Position.Fields.PositionName].ToString();
                                t.Competency.CompetncyDesc = dr[Competency.Fields.CompetncyDesc] == DBNull.Value ? string.Empty : dr[Competency.Fields.CompetncyDesc].ToString();
                                t.LevelCompetency.Level = dr[LevelCompetency.Fields.Level] == DBNull.Value ? (short?)null : short.Parse(dr[LevelCompetency.Fields.Level].ToString());

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }


        public bool Save(ManagerialKeyBehaviour domain, List<ManagerialKeyBehaviourDetail> list)
        {
            bool result = false;

            using (SqlConnection conn = new SqlConnection(connectionString))
            {

                conn.Open();
                SqlTransaction txn = conn.BeginTransaction();
                try
                {
                    #region Insert Data ManagerialKeyBehaviour

                    //string query = "INSERT INTO master.Competency(RowStatus, CompetncyDesc, Definition, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate) VALUES(@RowStatus, @CompetncyDesc, @Definition, @CreatedBy, @CreatedDate, @UpdatedBy, @UpdatedDate)";

                    SqlCommand cmd = new SqlCommand("SP_ManagerialKeyBehaviour_Insert", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@PositionID", SqlDbType.Int).Value = (object)domain.PositionID ?? DBNull.Value;
                    cmd.Parameters.Add("@CompetencyID", SqlDbType.Int).Value = (object)domain.CompetencyID ?? DBNull.Value;
                    cmd.Parameters.Add("@LevelCompetencyID", SqlDbType.Int).Value = (object)domain.LevelCompetencyID ?? DBNull.Value;
                    cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = (object)domain.CreatedDate ?? DBNull.Value;
                    cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = (object)domain.CreatedBy ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)domain.UpdatedDate ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)domain.UpdatedBy ?? DBNull.Value;

                    cmd.Transaction = txn;
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();

                    #endregion

                    #region Insert Data ManagerialKeyBehaviourDetail


                    #region Ambil ManagerialKeyBehaviourID


                    SqlCommand cmdKeyBehaviour = new SqlCommand("SP_ManagerialKeyBehaviour_SelectBy_CreatedByCreatedDate", conn);
                    cmdKeyBehaviour.CommandType = CommandType.StoredProcedure;
                    cmdKeyBehaviour.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = domain.CreatedBy;
                    cmdKeyBehaviour.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = domain.CreatedDate;
                    cmdKeyBehaviour.Transaction = txn;
                    var BehaviourID = cmdKeyBehaviour.ExecuteScalar();
                    int resultBehaviourID = BehaviourID == DBNull.Value ? 0 : int.Parse(BehaviourID.ToString());
                    cmdKeyBehaviour.Dispose();

                    #endregion


                    foreach (ManagerialKeyBehaviourDetail data in list)
                    {

                        SqlCommand cmdKeyBehaviourDetail = new SqlCommand("SP_ManagerialKeyBehaviourDetail_Insert", conn);
                        cmdKeyBehaviourDetail.CommandType = CommandType.StoredProcedure;
                        cmdKeyBehaviourDetail.Parameters.Add("@ManagerialKeyBehaviourID", SqlDbType.Int).Value = resultBehaviourID;
                        cmdKeyBehaviourDetail.Parameters.Add("@KeyBehaviourDetailID", SqlDbType.Int).Value = (object)data.KeyBehaviourDetailID ?? DBNull.Value;
                        cmdKeyBehaviourDetail.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = (object)data.CreatedDate ?? DBNull.Value;
                        cmdKeyBehaviourDetail.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = (object)data.CreatedBy ?? DBNull.Value;
                        cmdKeyBehaviourDetail.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)data.UpdatedDate ?? DBNull.Value;
                        cmdKeyBehaviourDetail.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)data.UpdatedBy ?? DBNull.Value;


                        cmdKeyBehaviourDetail.Transaction = txn;
                        cmdKeyBehaviourDetail.ExecuteNonQuery();
                        cmdKeyBehaviourDetail.Dispose();
                    }

                    #endregion


                    result = true;
                    txn.Commit();

                }
                catch (Exception ex)
                {
                    txn.Rollback();
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }

            return result;
        }

        public bool DeleteWithDetail(int key)
        {
            bool result = false;

            using (SqlConnection conn = new SqlConnection(connectionString))
            {

                conn.Open();
                SqlTransaction txn = conn.BeginTransaction();
                try
                {


                    #region Delete Table ManagerialKeyBehaviourDetail



                    SqlCommand cmdLevel = new SqlCommand("[SP_ManagerialKeyBehaviourDetail_Delete_ByManagerialKeyBehaviourID]", conn);
                    cmdLevel.CommandType = CommandType.StoredProcedure;
                    cmdLevel.Parameters.Add("@ManagerialKeyBehaviourID", SqlDbType.Int).Value = key;

                    cmdLevel.Transaction = txn;
                    cmdLevel.ExecuteNonQuery();
                    cmdLevel.Dispose();

                    #endregion

                    #region Delete Table ManagerialKeyBehaviour


                    SqlCommand cmd = new SqlCommand("SP_ManagerialKeyBehaviour_Delete", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ManagerialKeyBehaviourID", SqlDbType.Int).Value = key;

                    cmd.Transaction = txn;
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();

                    #endregion


                    result = true;
                    txn.Commit();

                }
                catch (Exception ex)
                {
                    txn.Rollback();
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }

            return result;
        }

        public List<ManagerialKeyBehaviour> GetListByPositionID(int PositionID)
        {
            List<ManagerialKeyBehaviour> tt = new List<ManagerialKeyBehaviour>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "SELECT ManagerialKeyBehaviourID, PositionID, CompetencyID, LevelCompetencyID, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM master.ManagerialKeyBehaviour";
                    cmd.CommandText = "SP_ManagerialKeyBehaviour_SelectByPositionID";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@PositionID", SqlDbType.Int).Value = PositionID;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                ManagerialKeyBehaviour t = new ManagerialKeyBehaviour(true);
                                t.ManagerialKeyBehaviourID = dr[ManagerialKeyBehaviour.Fields.ManagerialKeyBehaviourID] == DBNull.Value ? 0 : int.Parse(dr[ManagerialKeyBehaviour.Fields.ManagerialKeyBehaviourID].ToString());
                                t.PositionID = dr[ManagerialKeyBehaviour.Fields.PositionID] == DBNull.Value ? (int?)null : int.Parse(dr[ManagerialKeyBehaviour.Fields.PositionID].ToString());
                                t.CompetencyID = dr[ManagerialKeyBehaviour.Fields.CompetencyID] == DBNull.Value ? (int?)null : int.Parse(dr[ManagerialKeyBehaviour.Fields.CompetencyID].ToString());
                                t.LevelCompetencyID = dr[ManagerialKeyBehaviour.Fields.LevelCompetencyID] == DBNull.Value ? (int?)null : int.Parse(dr[ManagerialKeyBehaviour.Fields.LevelCompetencyID].ToString());
                                t.CreatedDate = dr[ManagerialKeyBehaviour.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[ManagerialKeyBehaviour.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[ManagerialKeyBehaviour.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[ManagerialKeyBehaviour.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[ManagerialKeyBehaviour.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[ManagerialKeyBehaviour.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[ManagerialKeyBehaviour.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[ManagerialKeyBehaviour.Fields.UpdatedBy].ToString();

                                t.Competency.CompetncyDesc = dr[Competency.Fields.CompetncyDesc] == DBNull.Value ? string.Empty : dr[Competency.Fields.CompetncyDesc].ToString();
                                
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }		
    }
}
